#include "Player.h"
#include "Runner.h"
#include "../SGD Wrappers/SGD_InputManager.h"
#include "../SGD Wrappers/SGD_Event.h"
#include "../SGD Wrappers/SGD_EventManager.h"
#include "../SGD Wrappers/SGD_AudioManager.h"
#include "../menu/DodgeBomb.h"

#include "Wall.h"
#include "../Punch.h"
#include "PowerUp.h"
#include "../Game.h"
#include "../tile/TileManager.h"
#include <math.h>
#include "../CreatePunchMessage.h"
#include "../source/Dodgeball.h"

#define CHARGE_RATE_MULTIPLIER 3.0f

CPlayer::CPlayer()
{
	Listener::RegisterForEvent("ATTACK_PLAYER");
	Listener::RegisterForEvent("HAZARD_SLOW");
	Listener::RegisterForEvent("HAZARD_DAMAGE");
	Listener::RegisterForEvent("TORNADO_SWIRL");
}


CPlayer::~CPlayer()
{
	Listener::UnregisterFromEvent("ATTACK_PLAYER");
	Listener::UnregisterFromEvent("HAZARD_SLOW");
	Listener::UnregisterFromEvent("HAZARD_DAMAGE");
	Listener::UnregisterFromEvent("TORNADO_SWIRL");
}

/*virtual*/ void CPlayer::HandleCollision(const Entity* pOther)	/*override*/
{


	CAgent::HandleCollision(pOther);

	if (pOther->GetType() == ENT_BALL && m_bPlayerStun == false)
	{
		if (m_bAmIInAMiniGame)
		{
			Entity* p = const_cast<Entity*>(pOther);
			if (dynamic_cast<CBall*>(p)->explosion == true)
			{
				this->ToggleActive(false);
				CDodgeBomb::GetInstance()->SetDeadCount(CDodgeBomb::GetInstance()->GetDeadCount() + 1);
			}

		}

		Entity* p = const_cast<Entity*>(pOther);
		if (m_bTakeDownRush == true)
		{
			m_nPowerLevel = 0;
			m_bTakeDownRush = false;
		}
		if (GetBall() == nullptr)
		{
			if (m_bDash == true && m_bAmIInAMiniGame == true)
			{
				SetBall(dynamic_cast<CBall*>(p));
				m_bCarrying = true;
				m_pBall->SetOwner(this);
			}
			if (pOther->GetAerial() == false)			
				SetBall(dynamic_cast<CBall*>(p));
			
			if (m_bExhausted == false && pOther->GetAerial() == false && dynamic_cast<CBall*>(p)->GetPowerLevel() == 0 && m_bBeingCarried == false && GetCarryDude() == nullptr)
			{
				m_bCarrying = true;
				m_pBall->SetOwner(this);
			}
			else if (dynamic_cast<CBall*>(p)->GetPowerLevel() != 0 && dynamic_cast<CBall*>(p)->GetOwner() == this && m_pBall->GetAerial() == false)
			{
				m_bCarrying = true;
				m_pBall->SetOwner(this);
				dynamic_cast<CBall*>(p)->SetPowerLevel(0);
			}
			else if (dynamic_cast<CBall*>(p)->GetPowerLevel() == 1 && dynamic_cast<CBall*>(p)->GetOwner() != this)
			{
				m_fCurrStamina -= 1;
				m_bPlayerStun = true;
				m_fStunTimer = 5;
				dynamic_cast<CBall*>(p)->SetPowerLevel(0);
			}
			else if (dynamic_cast<CBall*>(p)->GetPowerLevel() == 2 && dynamic_cast<CBall*>(p)->GetOwner() != this)
			{
				m_fCurrStamina -= 2;
				m_bPlayerStun = true;
				m_fStunTimer = 8;
				dynamic_cast<CBall*>(p)->SetPowerLevel(0);
			}
			else if (dynamic_cast<CBall*>(p)->GetPowerLevel() == 3 && dynamic_cast<CBall*>(p)->GetOwner() != this)
			{
				m_fCurrStamina -= 3;
				m_bPlayerStun = true;
				m_fStunTimer = 10;
				dynamic_cast<CBall*>(p)->SetPowerLevel(0);
			}
		}
		/*if (m_bAmIInAMiniGame == true && dynamic_cast<const CDodgeball*>(pOther)->explosion == true)
		{
		ToggleActive(false);
		}*/
	}

	if (pOther->GetType() == ENT_PUNCH)
	{
		Entity* p = const_cast<Entity*>(pOther);
		if (dynamic_cast<CPunch*>(p)->GetOwner() != this)
		{
			if (!SGD::AudioManager::GetInstance()->IsAudioPlaying(Game::GetInstance()->Gethit()))
				SGD::AudioManager::GetInstance()->PlayAudio(Game::GetInstance()->Gethit());

			m_fCurrStamina -= 1;
			//taking the knockback of the person that punched you and direction
			if (dynamic_cast<CPunch*>(p)->GetHeading() == true)
				m_ptPosition.x += dynamic_cast<CPlayer*>(dynamic_cast<CPunch*>(p)->GetOwner())->GetKnockBack();
			else
				m_ptPosition.x -= dynamic_cast<CPlayer*>(dynamic_cast<CPunch*>(p)->GetOwner())->GetKnockBack();
			if (m_pHitPar == nullptr)
			{
				hitTimer = 0.5f;
				m_pHitPar = CParticleManager::GetInstance()->CreateEmitter(&m_ptPosition, "Resources/Particles/Blood.xml", CEmitter::STEADY);
			}
		}
	}

	if (pOther->GetType() == ENT_PUP)
	{
		SGD::AudioManager::GetInstance()->PlayAudio(Game::GetInstance()->GetPowerupSound(), false);
		Entity* p = const_cast<Entity*>(pOther);
		if (dynamic_cast<CPowerUp*>(p)->GetTypeofPowerUp() == CPowerUp::PUP_KNOCKBACK)
		{
			m_nKnockBack += 20;
		}
		else if (dynamic_cast<CPowerUp*>(p)->GetTypeofPowerUp() == CPowerUp::PUP_STEAL)
		{
			m_nStealChance = 7;
		}
		else if (dynamic_cast<CPowerUp*>(p)->GetTypeofPowerUp() == CPowerUp::PUP_ABILITYCHARGE)
		{
			m_fAbilityCharge += 30;
		}

		//always set it to held and turn it off
		// set the owner to you
		p->SetHeld(true);
		p->ToggleActive(false);
		dynamic_cast<CPowerUp*>(p)->SetOwner(this);
	}

	if (pOther->GetType() == ENT_PLAYER)
	{
		Entity* player = const_cast<Entity*>(pOther);

		if (m_bDash == true)
		{

			if (dynamic_cast<CPlayer*>(player)->GetCarrying() == true)
			{
				int x = (int)(rand() % m_nStealChance) + 1;
				if (x == 1)
				{
					// they steal the ball
					dynamic_cast<CPlayer*>(player)->SetCarrying(false);
					m_bCarrying = true;
					dynamic_cast<CPlayer*>(player)->GetBall()->SetOwner(this);
					SetBall(dynamic_cast<CPlayer*>(player)->GetBall());
					dynamic_cast<CPlayer*>(player)->SetBall(nullptr);
					dynamic_cast<CPlayer*>(player)->SetCurrStamina(dynamic_cast<CPlayer*>(player)->GetCurrStamina() - 0.5f);
				}
				else
					dynamic_cast<CPlayer*>(player)->SetCurrStamina(dynamic_cast<CPlayer*>(player)->GetCurrStamina() - 0.5f);
			}
			else
				dynamic_cast<CPlayer*>(player)->SetCurrStamina(dynamic_cast<CPlayer*>(player)->GetCurrStamina() - 0.5f);

			if (!SGD::AudioManager::GetInstance()->IsAudioPlaying(Game::GetInstance()->GetTackleSound()))
				SGD::AudioManager::GetInstance()->PlayAudio(Game::GetInstance()->GetTackleSound());
		}


		if (GetThrownAbility())
		{

			if (dynamic_cast<CPlayer*>(player)->GetCarrying() == false)
			{

				SetCarryDude(dynamic_cast<CPlayer*>(player));
				GetCarryDude()->SetBeingCarried(true);
				if (m_bThrowTheGuy == true)
				{
					if (m_nPowerLevel == 1)
					{
						m_nPowerLevel = 0;
						m_bThrowAbility = false;
						GetCarryDude()->SetStun(true);
						GetCarryDude()->SetStunTimer(0.4f);
						GetCarryDude()->SetVelocity(GetThrownGuy());
						GetCarryDude()->SetBeingCarried(false);


						if (GetTeamMate() != GetCarryDude())
						{
							GetCarryDude()->SetStunTimer(3);


						}
						SetCarryDude(nullptr);
					}
					else if (m_nPowerLevel == 2)
					{
						m_nPowerLevel = 0;
						m_bThrowAbility = false;
						GetCarryDude()->SetStun(true);
						GetCarryDude()->SetStunTimer(0.8f);
						GetCarryDude()->SetVelocity(GetThrownGuy());
						GetCarryDude()->SetBeingCarried(false);

						if (GetTeamMate() != GetCarryDude())
						{
							GetCarryDude()->SetStunTimer(3);
							GetCarryDude()->SetCurrStamina(GetCarryDude()->GetCurrStamina() - 5);
						}
						SetCarryDude(nullptr);
					}
					else if (m_nPowerLevel == 3)
					{
						m_nPowerLevel = 0;
						m_bThrowAbility = false;
						GetCarryDude()->SetStun(true);
						GetCarryDude()->SetStunTimer(2);
						GetCarryDude()->SetVelocity(GetThrownGuy());
						GetCarryDude()->SetBeingCarried(false);

						if (GetTeamMate() != GetCarryDude())
						{
							GetCarryDude()->SetStunTimer(3);
							GetCarryDude()->SetCurrStamina(GetCarryDude()->GetCurrStamina() - 10);
						}

						SetCarryDude(nullptr);
					}

					m_bThrowTheGuy = false;
				}

			}
		}

		if (m_bTakeDownRush == true)
		{

			if (m_nPowerLevel == 1 && GetTeam() != dynamic_cast<CPlayer*>(player)->GetTeam())
			{
				dynamic_cast<CAgent*>(player)->SetCurrStamina(dynamic_cast<CAgent*>(player)->GetCurrStamina() - 10);
			}
			else if (m_nPowerLevel == 2 && GetTeam() != dynamic_cast<CPlayer*>(player)->GetTeam())
			{
				dynamic_cast<CAgent*>(player)->SetCurrStamina(dynamic_cast<CAgent*>(player)->GetCurrStamina() - 20);
			}
			else if (m_nPowerLevel == 3 && GetTeam() != dynamic_cast<CPlayer*>(player)->GetTeam())
			{
				dynamic_cast<CAgent*>(player)->SetCurrStamina(dynamic_cast<CAgent*>(player)->GetCurrStamina() - 30);
			}
			m_nPowerLevel = 0;
			m_bTakeDownRush = false;
		}

	}



	if (pOther->GetType() == ENT_WALL)
	{
		if (m_bTakeDownRush == true)
		{
			if (m_nPowerLevel == 1)
			{
				if (m_vtVelocity.x > 0 && m_vtVelocity.x > m_vtVelocity.y)
					m_ptPosition.x -= 50;
				else if (m_vtVelocity.x < 0 && m_vtVelocity.x < m_vtVelocity.y)
					m_ptPosition.x += 50;

				if (m_vtVelocity.y > 0 && m_vtVelocity.y > m_vtVelocity.x)
					m_ptPosition.y -= 50;
				else if (m_vtVelocity.y < 0 && m_vtVelocity.y < m_vtVelocity.x)
					m_ptPosition.y += 50;

				m_nPowerLevel = 0;
				m_bTakeDownRush = false;
			}
			else if (m_nPowerLevel == 2)
			{
				Entity* wall = const_cast<Entity*>(pOther);

				if (dynamic_cast<CWall*>(wall)->GetHeight())
				{
					if (m_vtVelocity.x > 0 && m_vtVelocity.x > m_vtVelocity.y)
						m_ptPosition.x -= 50;
					else if (m_vtVelocity.x < 0 && m_vtVelocity.x < m_vtVelocity.y)
						m_ptPosition.x += 50;

					if (m_vtVelocity.y > 0 && m_vtVelocity.y > m_vtVelocity.x)
						m_ptPosition.y -= 50;
					else if (m_vtVelocity.y < 0 && m_vtVelocity.y < m_vtVelocity.x)
						m_ptPosition.y += 50;
				}
				else
				{
					SGD::Event* turnOff = new SGD::Event("TURN_OFF");
					turnOff->SendEventNow(pOther);
					delete turnOff;
					turnOff = nullptr;
				}

				m_nPowerLevel = 0;
				m_bTakeDownRush = false;
			}
			else if (m_nPowerLevel == 3)
			{
				SGD::Event* turnOff = new SGD::Event("TURN_OFF");
				turnOff->SendEventNow(pOther);
				delete turnOff;
				turnOff = nullptr;

				if (dynamic_cast<const CWall*>(pOther)->GetImpenetrable() == true)
				{
					m_nPowerLevel = 0;
					m_bTakeDownRush = false;
				}
			}
		}
	}

	if (pOther->GetType() == ENT_GORBI)
	{
		if (m_bTakeDownRush == true)
		{
			Entity* enemy = const_cast<Entity*>(pOther);

			if (m_nPowerLevel == 1)
			{
				dynamic_cast<CAgent*>(enemy)->SetCurrStamina(dynamic_cast<CAgent*>(enemy)->GetCurrStamina() - 10);
			}
			else if (m_nPowerLevel == 2)
			{
				dynamic_cast<CAgent*>(enemy)->SetCurrStamina(dynamic_cast<CAgent*>(enemy)->GetCurrStamina() - 20);
			}
			else if (m_nPowerLevel == 3)
			{
				dynamic_cast<CAgent*>(enemy)->SetCurrStamina(dynamic_cast<CAgent*>(enemy)->GetCurrStamina() - 30);
			}
			m_nPowerLevel = 0;
			m_bTakeDownRush = false;
		}

	}

	if (pOther->GetType() == ENT_CANNYBLE)
	{
		if (m_bTakeDownRush == true)
		{
			Entity* enemy = const_cast<Entity*>(pOther);

			if (m_nPowerLevel == 1)
			{
				dynamic_cast<CAgent*>(enemy)->SetCurrStamina(dynamic_cast<CAgent*>(enemy)->GetCurrStamina() - 10);
			}
			else if (m_nPowerLevel == 2)
			{
				dynamic_cast<CAgent*>(enemy)->SetCurrStamina(dynamic_cast<CAgent*>(enemy)->GetCurrStamina() - 20);
			}
			else if (m_nPowerLevel == 3)
			{
				dynamic_cast<CAgent*>(enemy)->SetCurrStamina(dynamic_cast<CAgent*>(enemy)->GetCurrStamina() - 30);
			}

			m_nPowerLevel = 0;
			m_bTakeDownRush = false;
		}

	}


}

void CPlayer::Update(float elapsedTime) /*override*/
{
	SGD::InputManager* pInput = SGD::InputManager::GetInstance();

	if (hitTimer > 0.0f)
		hitTimer -= elapsedTime;

	if (hitTimer <= 0.0f)
	{
		if (m_pHitPar != nullptr)
		{
			CParticleManager::GetInstance()->DeleteEmitter(m_pHitPar);
			m_pHitPar = nullptr;
		}
	}
	//unlimited stamina in min-games.
	if (Game::GetInstance()->GetMiniGame())
	{
		m_fCurrStamina = m_fMaxStamina;
	}


	KeepInWorld();

	m_fChargeTimer = elapsedTime;
	m_fDashTimer -= elapsedTime;

	if (GetCarryDude() != nullptr)
	{
		if (m_bIsHeadingRight == true)
			GetCarryDude()->SetPosition({ m_ptPosition.x + GetSize().width + 2, m_ptPosition.y - GetCarryDude()->GetSize().height + 5 });
		else
			GetCarryDude()->SetPosition({ m_ptPosition.x - GetCarryDude()->GetSize().width - 2, m_ptPosition.y - GetCarryDude()->GetSize().height + 5 });
	}

	if (m_bPlayerStun)
		m_fStunTimer -= elapsedTime;

	if (m_bTakeDownRush)
		m_vRushTimer -= elapsedTime;

	if (m_vRushTimer <= 0)
		m_bTakeDownRush = false;

	if (m_fStunTimer < 0)
		m_bPlayerStun = false;

	// Ability meter:
	//	- level 1 = 10
	//	- level 2 = 30
	//	- level 3 = 60
	//No to infinite abilty meter!
	if (m_fAbilityCharge > 60)
	{
		//Cap it!
		m_fAbilityCharge = 60;
	}
	m_fAbilityCharge += elapsedTime - (elapsedTime * 0.5f);

	if (m_fCurrStamina <= 0.01f)
	{
		m_bExhausted = true;
		if (!SGD::AudioManager::GetInstance()->IsAudioPlaying(Game::GetInstance()->GetExhaustedSound()))
			SGD::AudioManager::GetInstance()->PlayAudio(Game::GetInstance()->GetExhaustedSound());
	}


	if (m_fCurrStamina >= 60)
		m_bExhausted = false;

	/*if (m_pEmitter != nullptr)
		m_pEmitter->m_poiPos = m_ptPosition;*/

	if (m_bCarrying == true)
		m_pBall->SetPosition(m_ptPosition);


	if (m_fCurrStamina < 0)
		m_fCurrStamina = 0;




	SGD::Event* assesThreat = new SGD::Event("ASSESS_THREAT", nullptr, this);
	SGD::EventManager::GetInstance()->QueueEvent(assesThreat);

	if (m_bCarrying == true && m_bExhausted == true)
	{
		m_bCarrying = false;
		m_pBall->SetHeld(false);
		m_pBall->ToggleActive(true);
		SetBall(nullptr);
	}

	if (m_fDashTimer <= 0)
	{
		m_bDash = false;
	}



	CAgent::Update(elapsedTime);
}



void CPlayer::Render(void)
{

	SGD::Rectangle temp = GetAbilityRect();
	temp.right = temp.left;
	temp.right = m_fAbilityCharge*3.0f;
	//Ability meters filled section

	//fixing the rect if We put the HUD far to the right.
	if (temp.right < temp.left)
	{
		temp.right = temp.left + temp.right;
	}
	SGD::GraphicsManager::GetInstance()->DrawRectangle(temp, { 0, 0, 255 });
	temp.right = 10.0f*CHARGE_RATE_MULTIPLIER;

	//fixing the rect if We put the HUD far to the right.
	if (temp.right < temp.left)
	{
		temp.right = temp.left + temp.right;
	}

	SGD::GraphicsManager::GetInstance()->DrawRectangle(temp, { 0, 0, 0, 0 }, { 255, 0, 0, 0 });
	temp.right = 30.0f*CHARGE_RATE_MULTIPLIER;

	//fixing the rect if We put the HUD far to the right.
	if (temp.right < temp.left)
	{
		temp.right = temp.left + temp.right;
	}

	SGD::GraphicsManager::GetInstance()->DrawRectangle(temp, { 0, 0, 0, 0 }, { 255, 0, 0, 0 });
	temp.right = 60.0f*CHARGE_RATE_MULTIPLIER;

	//fixing the rect if We put the HUD far to the right.
	if (temp.right < temp.left)
	{
		temp.right = temp.left + temp.right;
	}

	SGD::GraphicsManager::GetInstance()->DrawRectangle(temp, { 0, 0, 0, 0 }, { 255, 0, 0, 0 });

	if (m_fCharging > 1)
	{
		SGD::Rectangle t = GetAbilityRect();
		t.right = t.left;
		t.right = m_fCharging*3.0f;

		if (t.right < t.left)
		{
			t.right = t.left + t.right;
		}
		SGD::GraphicsManager::GetInstance()->DrawRectangle(t, { 255, 255, 0 });
	}
}

void CPlayer::Input(float speed)
{
	SGD::InputManager* pInput = SGD::InputManager::GetInstance();
	SGD::Vector LeftAnalog = pInput->GetLeftJoystick(GetControllernumber());
	//testing stuff, REMOVE THIS LATER!
	if (SGD::InputManager::GetInstance()->IsKeyPressed(SGD::Key::NumPad1))
	{
		m_fAbilityCharge = 7.0f;
	}
	if (SGD::InputManager::GetInstance()->IsKeyPressed(SGD::Key::NumPad2))
	{
		m_fAbilityCharge = 27.0f;
	}
	if (SGD::InputManager::GetInstance()->IsKeyPressed(SGD::Key::NumPad3))
	{
		m_fAbilityCharge = 57.0f;
	}


	if (m_bPlayerStun == false)
	{
		float LX = LeftAnalog.x;
		float LY = LeftAnalog.y;

		float x1 = LX;
		float y1 = LY;

		float dx = 4 * x1;
		float dy = 4 * y1;

		SGD::Vector v = { dx, dy };

		if (m_bTakeDownRush == false)
		{
			m_fRotation = atan2f(-LX, LY);

			v *= speed;

			m_vtVelocity = v;
			m_vRushVel = v;
		}
		else
		{
			m_vtVelocity = m_vRushVel + (v * 20);
		}



		float trigger = pInput->GetTrigger(GetControllernumber());

		if (LX > 0.5)
		{
			m_bIsHeadingRight = true;
		}

		if (LX < 0)
		{
			m_bIsHeadingRight = false;
		}


		//right bumper
		if (pInput->IsButtonDown(GetControllernumber(), 5))
		{
			if (m_bExhausted == false)

			{
				m_fCurrStamina -= 0.01f;
				m_bSprinting = true;

			}
		}
		else
		{

			if (m_bDash == false)

			{
				if (m_fCurrStamina < m_fMaxStamina)
					m_fCurrStamina += 0.001f;

			}
			m_bSprinting = false;
		}

		if (pInput->IsButtonReleased(GetControllernumber(), 5))
		{
			if (m_pEmitter != nullptr)
			{
				CParticleManager::GetInstance()->DeleteEmitter(m_pEmitter);
				m_pEmitter = nullptr;
			}
		}

		if (pInput->IsButtonPressed(GetControllernumber(), 5))
		{
			if (m_bExhausted == false && m_bCarrying == false)
			{
				
				SGD::AudioManager::GetInstance()->PlayAudio(Game::GetInstance()->GetSpriting());

				m_pEmitter = CParticleManager::GetInstance()->CreateEmitter(&m_ptPosition, "Resources/Particles/Sprint.xml", CEmitter::STEADY);
			}
		}

		// left bumper dash/tackle
		if (pInput->IsButtonPressed(GetControllernumber(), 4) && m_bCarrying == false)
		{
			if (m_bExhausted == false)
			{
				m_fCurrStamina -= 5;
				m_bDash = true;
				m_fDashTimer = 0.08f;

				SGD::AudioManager::GetInstance()->PlayAudio(Game::GetInstance()->GetDashSound());
			}
		}


		if (m_fCooldown >= 130.0f)

		{

			//X button down
			if (pInput->IsButtonDown(GetControllernumber(), 2))

			{

				if (m_fAbilityCharge > 9)
					StartCharge();

			}

			//X button released
			if (pInput->IsButtonReleased(GetControllernumber(), 2))

			{
				//before sending the button make sure
				//they actually have enough ability 
				//to actually use a ability
				if (m_fAbilityCharge > 9)
					m_nButton = 2;


			}

			//Y button down
			if (pInput->IsButtonDown(GetControllernumber(), 3))

			{

				if (m_fAbilityCharge > 9)
					StartCharge();

			}

			//Y button released
			if (pInput->IsButtonReleased(GetControllernumber(), 3))

			{
				//before sending the button make sure
				//they actually have enough ability 
				//to actually use a ability
				if (m_fAbilityCharge > 9)
					m_nButton = 3;

				SetCooldown(0);
			}
		}

		// triggers are not down
		if (trigger == 0)
		{

			if (m_fThrowCharge != 0.0f)
			{

				if (m_bCarrying)
				{

					if (m_fThrowCharge >= 0.3f)
					{
						Throw(v);
					}
					else
					{
						SGD::Vector throwVec = m_pTeamMate->GetPosition() - m_ptPosition;
						throwVec.Normalize();


						throwVec *= 50;


						Throw(throwVec);


					}
					m_fThrowCharge = 0.0f;
				}
			}
			m_fThrowCharge = 0.0f;
		}

		//right trigger

		if (trigger > 0)
		{
			m_fThrowCharge += m_fChargeTimer;
			if (GetThrownAbility())
			{
				m_bThrowTheGuy = true;
				m_vThrownGuy = v;
			}
			if (m_bAmIInAMiniGame && GetBall() != nullptr)
			{
				Throw(v);
			}
		}

		//left trigger
		if (trigger < 0.0f)
		{
			CAgent::Punch();
		}
	}
}


void CPlayer::Sprint()
{

}

void CPlayer::Tackle()
{

}

void CPlayer::Throw(SGD::Vector throwVec)
{
	if (throwVec.x < 250 && throwVec.x > 0)
	{
		throwVec.x = 250;
	}

	if (throwVec.x < 0 && throwVec.x > -250)

	{
		throwVec.x = -250;

	}

	if (throwVec.x > 300)

	{
		throwVec.x = 300;

	}

	if (throwVec.x < -300)
	{
		throwVec.x = -300;

	}

	if (throwVec.y < 250 && throwVec.y > 0)

	{
		throwVec.y = 250;
	}


	if (throwVec.y < 0 && throwVec.y > -250)
	{
		throwVec.y = -250;
	}


	if (throwVec.y > 300)
	{
		throwVec.y = 300;
	}

	if (throwVec.y < -300)
	{
		throwVec.y = -300;
	}

	if (m_bAmIInAMiniGame)
	{
		//throwVec.Normalize();
		throwVec *= 10;
	}
	m_pBall->SetDestPos({ m_ptPosition.x + throwVec.x, m_ptPosition.y + throwVec.y });
	m_pBall->SetVelocity({ (throwVec.x / 2) + 10, (throwVec.y / 2) + 10 });
	m_pBall->SetAerialVel({ m_pBall->GetVelocity().x, m_pBall->GetVelocity().y - 50 });
	m_pBall->SetShadowPos({ m_pBall->GetPosition().x, m_pBall->GetPosition().y });
	m_pBall->SetAerial(true);
	m_bCarrying = false;
	SetBall(nullptr);

	SGD::AudioManager::GetInstance()->PlayAudio(Game::GetInstance()->GetThrowingSound());
}

/*virtual*/ void CPlayer::HandleEvent(const SGD::Event* pEvent) /*override*/
{
	if (pEvent->GetEventID() == "ATTACK_PLAYER")
	{
		float* damage = reinterpret_cast<float*>(pEvent->GetData());

		if (damage != nullptr)
			m_fCurrStamina -= *damage;
	}
	if (pEvent->GetEventID() == "HAZARD_SLOW")
	{
		m_fSlowEffect = HZ_SLOW;
		m_fSlowTime += 0.0001f;
	}
	if (pEvent->GetEventID() == "HAZARD_DAMAGE")
	{
		m_fDamageOverTime = HZ_DAMAGE;
		m_fDOTTime += 0.0001f;
	}

	if (pEvent->GetEventID() == "TORNADO_SWIRL")
	{
		if (pEvent->GetSender() != this)
		{

			switch (reinterpret_cast<int>(pEvent->GetData()))
			{
			case 1:
			{
					  //check how far I am so I can get effected by the tornado
					  if (SGD::Vector(reinterpret_cast<CRunner*>(pEvent->GetSender())->GetTornadoPos() - this->GetPosition()).ComputeLength() > 100)
						  this->RotatePlayers(this, 0.005f, reinterpret_cast<CRunner*>(pEvent->GetSender())->GetTornadoPos());
			}
				break;
			case 2:
			{
					  //check how far I am so I can get effected by the tornado
					  if (SGD::Vector(reinterpret_cast<CRunner*>(pEvent->GetSender())->GetTornadoPos() - this->GetPosition()).ComputeLength() > 150)
						  this->RotatePlayers(this, 0.007f, reinterpret_cast<CRunner*>(pEvent->GetSender())->GetTornadoPos());
			}
				break;
			case 3:
			{
					  //check how far I am so I can get effected by the tornado
					  if (SGD::Vector(reinterpret_cast<CRunner*>(pEvent->GetSender())->GetTornadoPos() - this->GetPosition()).ComputeLength() > 250)
						  this->RotatePlayers(this, 0.01f, reinterpret_cast<CRunner*>(pEvent->GetSender())->GetTornadoPos());
			}
				break;
			default:
				break;
			}
		}
	}
}

void CPlayer::StartCharge()
{
	m_fCharging += m_fChargeTimer * 5;
	if (m_fCharging >= m_fAbilityCharge)
	{
		m_fCharging = m_fAbilityCharge;
	}
}

void CPlayer::KeepInWorld()
{
	//dont go too far left
	if (GetRect().left <= 0)
		this->m_ptPosition.x = 1;

	//dont go too far up
	if (GetRect().top <= 0)
		this->m_ptPosition.y = 1;

	//dont go too far right
	if (GetRect().left + GetSize().width > Game::GetInstance()->GetWorldWidth())
		this->m_ptPosition.x = Game::GetInstance()->GetWorldWidth() - GetSize().width;

	//dont go too far down
	if (GetRect().bottom > Game::GetInstance()->GetWorldHeight())
		this->m_ptPosition.y = Game::GetInstance()->GetWorldHeight() - GetSize().height;

}
